// pages/show-rooms-page/show-rooms-page.js
Page({
    data: {
        showLoadingState:false,
        //运动项目
        sportsNum:0,
        sportsArray:['所有项目','足球','篮球','排球','羽毛球','乒乓球','棒球','网球'],
        //房间搜索
        p1:"https://pic.imgdb.cn/item/62e22d95f54cd3f937ddbcb6.png",
        p3:"https://pic.imgdb.cn/item/62f9aa2a16f2c2beb174caea.png",
        roomid:123,
        //房间展示
        sportsPicUrlArray:[
            "https://pic.imgdb.cn/item/632e762116f2c2beb18e9710.png",
            "https://pic.imgdb.cn/item/632e762216f2c2beb18e97c6.png",
            "https://pic.imgdb.cn/item/632e762216f2c2beb18e9840.png",
            "https://pic.imgdb.cn/item/632e762316f2c2beb18e996b.png",
            "https://pic.imgdb.cn/item/632e762016f2c2beb18e95d0.png",
            "https://pic.imgdb.cn/item/632e764e16f2c2beb18ec96f.png",
            "https://pic.imgdb.cn/item/632e764e16f2c2beb18ec98d.png",
            "https://pic.imgdb.cn/item/632e764e16f2c2beb18ec955.png",],
        rooms:[
            //这是一个room结构体数组，每个room都是这样的结构
            {
                room_id:123156156, 
                room_location:'大活乒乓球馆',
                room_playground:'场地一',
                room_date:{
                    year:2022,
                    month:7,
                    day:10,
                    starttime:16,
                    endtime:20,
                },
                lack_person:5,
                room_sports:1,//假设1代表乒乓球
            },
            {
                room_id:123,
                room_location:'大活乒乓球馆',
                room_playground:'场地一',
                room_date:{
                    year:2022,
                    month:7,
                    day:10,
                    starttime:16,
                    endtime:20,
                },
                lack_person:5,
                room_sports:2,//假设1代表乒乓球
            },
            {
                room_id:123,
                room_location:'大活乒乓球馆',
                room_playground:'场地一',
                room_date:{
                    year:2022,
                    month:7,
                    day:10,
                    starttime:16,
                    endtime:20,
                },
                lack_person:5,
                room_sports:3,//假设1代表乒乓球
            },
            {
                room_id:123,
                room_location:'大活乒乓球馆',
                room_playground:'场地一',
                room_date:{
                    year:2022,
                    month:7,
                    day:10,
                    starttime:16,
                    endtime:20,
                },
                lack_person:5,
                room_sports:4,//假设1代表乒乓球
            },
            {
                room_id:123,
                room_location:'大活乒乓球馆',
                room_playground:'场地一',
                room_date:{
                    year:2022,
                    month:7,
                    day:10,
                    starttime:16,
                    endtime:20,
                },
                lack_person:5,
                room_sports:5,//假设1代表乒乓球
            },
            {
                room_id:123,
                room_location:'大活乒乓球馆',
                room_playground:'场地一',
                room_date:{
                    year:2022,
                    month:7,
                    day:10,
                    starttime:16,
                    endtime:20,
                },
                lack_person:5,
                room_sports:6,//假设1代表乒乓球
            },
            {
                room_id:123,
                room_location:'大活乒乓球馆',
                room_playground:'场地一',
                room_date:{
                    year:2022,
                    month:7,
                    day:10,
                    starttime:16,
                    endtime:20,
                },
                lack_person:5,
                room_sports:1,//假设1代表乒乓球
            },
        ],
    },
    onLoad(options) {
        var x = options.sportsNum;
        this.setData({
            sportsNum:x,
        });
        var that = this;
        wx.request({
            url: getApp().globalData.serverUrl,
            method:"GET",
            timeout:10000,
            data:{
              //各种数据的传送
              operation:"SHOW_ROOM",
              sportsNum:that.data.sportsNum,
            },
            success(res){
              if(res.data == false){
                  wx.showModal({
                      showCancel:false,
                      title:"请求失败,请重试",
                  });
              }
              else{
                  //成功获取到信息后的操作
                  that.setData({
                    'rooms':res.data.roomList,
                  });
              }
            },
            fail(res){
              wx.showModal({
                  showCancel:false,
                  title:"请求超时,请重试",
              });
            },
          });
  
    },
    //运动选择
    onChangeSports:function(e){
            var x = e.detail.value;
            this.setData({
                [`sportsNum`]: x,
            });

            //修改完了以后还需要上传并修改rooms
            var that = this;
            wx.request({
                url: getApp().globalData.serverUrl,
                method:"GET",
                timeout:150,
                data:{
                  //各种数据的传送
                  operation:"SHOW_ROOM",
                  sportsNum:that.data.sportsNum,
                },
                success(res){
                  if(res.data == false){
                      wx.showModal({
                          showCancel:false,
                          title:"请求失败,请重试",
                      });
                  }
                  else{
                      //成功获取到信息后的操作
                      that.setData({
                        'rooms':res.data.roomList,
                      });
                  }
                },
                fail(res){
                  wx.showModal({
                      showCancel:false,
                      title:"请求超时,请重试",
                  });
                },
            });
    },
    //搜索房间
    onSearchRoom:function(e){
        //连接服务器传递数据
        var that = this;
        that.onShowLoading()
        wx.request({
            url: getApp().globalData.serverUrl,
            method:"GET",
            timeout:10000,
            data:{
              //各种数据的传送
              operation:"SEARCH_ROOM",
              roomId:that.data.roomid,
            },
            success(res){
              if(res.data == false){
                  that.onHideLoading()
                  wx.showModal({
                      showCancel:false,
                      title:"请求失败,请重试",
                  });
              }
              else{
                  //成功获取到信息后的操作
                  console.log(res)
                  that.onHideLoading()
                  that.data.rooms.splice(1);
                  that.setData({
                    [`rooms`]:res.data.roomList,
                  });
              }
            },
            fail(res){
                that.onHideLoading()
              wx.showModal({
                  showCancel:false,
                  title:"请求超时,请重试",
              });
            },
        });
        //修改rooms

    },
    onChangeRoomid:function(e){
        var that = this;
        var x = e.detail.value;
        that.setData({
            'roomid':x,
        });
    },
    toPage_detailed_room_page:function(e){
        var roomid = e.currentTarget.dataset.roomid;
        wx.navigateTo({
          url: '/pages/detailed-room-page/detailed-room-page?roomid='+roomid,
        })
    },
    onShowLoading:function(e){
        var that = this;
        wx.showLoading({
          title: '加载中',
          mask:true,
          success(res){
              that.setData({
                  "showLoadingState":true,
              })
          }
        })
        setTimeout(that.onHideLoading,10200)
    },
    onHideLoading:function(e){
        var that = this;
        if(that.data.showLoadingState == true){
            wx.hideLoading();
            that.setData({ 
                "showLoadingState":false,
            })
        }
    },
})